user_list = {'777': {'密码': '123456', '余额': '100'},
             'yxy': {'密码': '123', '余额': '1'}}
dict_1 = {'name': '苹果', 'price': 1.2}
dict_2 = {'name': '香蕉', 'price': 5.5}
dict_3 = {'name': '西瓜', 'price': 0.8}

commodity_list = {'F01': dict_1,
                  'F02': dict_2,
                  'F03': dict_3}

have_bought_list = []


def main():
    global user_name, user_passwd
    print("欢迎来到三乐购物系统，请验证你的个人信息".center(50, "-"))
    user_name = input("请输入你的用户名:")
    if user_name in user_list:
        user_passwd = input("请输入你的密码:")
        if user_passwd == user_list[user_name]["密码"]:
            print("您已成功登录三乐购物系统，请尽情选择您需要的商品！")
            print("您的账户所剩余额是:", user_list[user_name]["余额"])
            menu_2(user_name)
        else:
            print("你的密码有误")
            choice()


def choice():
    choice_1 = input("如需继续登录请输入1\n如需退出请输入exit:")
    if choice_1 == "1":
        return main()
    elif choice_1 == "exit":
        print("已为您退出系统，欢迎下次光临")
    else:
        print("您的输入有误!")
        return choice()


def menu_2(user_name):
    print("1.查看商品信息并购买\n"
          "2.查看已购买信息\n"
          "3.充值\n"
          "4.退出\n")
    choice_1 = input("请选择并输入:")
    if choice_1 == "1":
        for i, j in commodity_list.items():
            print(f"商品编号:{i} 商品名:{commodity_list[i]['name']} 单价{commodity_list[i]['price']}/斤")
            print("1.商品选购\n"
                  "2.返回上一级\n"
                  "3.退出\n")
            choice_2 = input("请输入你的选择:")
            if choice_2 == "1":
                buy_1 = input("请选择你所要购买的商品编号:")
                if buy_1 in commodity_list.keys():
                    buy_count = int(input("请选择要购买商品的数量:"))
                    if buy_count % 1 == 0:
                        total_price = commodity_list[buy_1]['price'] * buy_count
                        print(f"你所选择的商品总价格是{total_price}元")
                        choice_3 = input("请确认是否支付(输入是或否):")
                        if choice_3 == "是":
                            if user_list[user_name]['余额'] >= total_price:
                                user_list[user_name]['余额'] = float(user_list[user_name]['余额']) - total_price
                                have_bought_list.append(
                                    {'商品名': commodity_list[i]['name'],
                                     '所购买的商品数': buy_count,
                                     '总价': total_price})
                            else:
                                print("您的余额不足\n"
                                      "1.充值"
                                      "2.退出")
                                choice_4 = input("请选择:")
                                if choice_4 == "1":
                                    while True:
                                        money = float(input("请输入你要充值的金额:"))
                                        user_list[user_name]['余额'] = float(user_list[user_name]['余额']) + money
                                        print(f"你已经成功充值,所剩余额为{user_list[user_name]['余额']}")
                                        print("自动为您跳回到上一级页面")
                                        return menu_2(user_name)
                                elif choice_4 == "2":
                                    print("已为您退出")
                                    break
                                else:
                                    print("请输入1或2")
            elif choice_2 == "2":
                print("已为您返回到上一级")
                menu_2(user_name)
            elif choice_2 == "3":
                print("已退出")
                break
    elif choice_1 == "2":
        if have_bought_list == '':
            print("你还未购买任何物品")
        else:
            print(have_bought_list)
    elif choice_1 == "3":
        while True:
            money = int(input("请输入你要充值的金额:"))
            if money % 1 == 0:
                user_list[user_name]['余额'] = user_list[user_name]['余额'] + money
                print(f"你已经成功充值,所剩余额为{user_list[user_name]['余额']}")
                print("自动为您跳回到上一级页面")
                return menu_2(user_name)
            else:
                print("请输入整数金额")


def menu():
    print("欢迎来到三乐购物系统".center(50, "-"))
    print("1.登录")
    print("2.退出")
    choice_5 = input("请输入你要执行的操作:")
    if choice_5 == "1":
        return main()
    elif choice_5 == "2":
        print("已为您退出购物系统，欢迎下次光临！")
    else:
        print("输入不正确，请重新输入！")
        return main()


main()
